/*
 * Created on Jan 23, 2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.vectrics.client.module;


import java.util.Iterator;
import java.util.Vector;
import java.util.Collection;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.VectricsException;
/**
 * @author mmoore
 */
public class ModuleManager {
	static private transient Logger log = Logger.getLogger(ModuleManager.class);
	static private ModuleManager instance = null; 
	private JComponent moduleParentPanel = null;
	private JComponent moduleNavigatorPanel = null;
	private Vector modules = new Vector();
	private Module previousModule = null;	
	private Module activeModule = null;
	
	public ModuleManager(JComponent modulePanel, JComponent moduleNavigatorPanel) {
		super();
		this.moduleParentPanel = modulePanel;
		this.moduleParentPanel.setLayout(new BoxLayout(this.moduleParentPanel, BoxLayout.X_AXIS));
		this.moduleNavigatorPanel = moduleNavigatorPanel;
		this.moduleNavigatorPanel.setLayout(new BoxLayout(this.moduleNavigatorPanel, BoxLayout.X_AXIS));
		//this.moduleNavigatorPanel.setLayout(new BoxLayout(this.moduleNavigatorPanel, BoxLayout.X_AXIS));
		instance = this;
	}
	
	static public ModuleManager getInstance() {
		if (instance == null) {
			log.error("ModuleManager's construct should be called before trying to acquire the instance");
		}
		return(instance);
	}
	
	public Collection getModuleList() {
		return(modules);
	}
	public void addModule(Module module) {
		modules.add(module);
	}
	
	public void setModules(Collection modules) {
		this.modules.clear();
		this.modules.addAll(modules);
	}

	public Module findModule(Class moduleClass) throws UnknownModuleException {
		log.debug("findModule(Class)");
		Iterator iter = modules.iterator();
		Module foundModule = null;
		while ((foundModule == null) && (iter.hasNext())) {
			Module currentModule = (Module)iter.next();
			if (currentModule.getClass().equals(moduleClass)) {
				foundModule = currentModule;
			}
			else {
				foundModule = currentModule.findModuleChild(moduleClass);
			}
		}
		return(foundModule);
	}
	

	public Module showModule(Class moduleClass) throws VectricsException {
		Module foundModule = findModule(moduleClass);
		if (foundModule != null) {
			showModule(foundModule);
		}
		else {
			throw new UnknownModuleException("Could not find module with class name = " + moduleClass.getName());
		}
		return(foundModule);
	}

	public void showModule(Module module) {
		log.debug("showModule() - showing module: " + module.getName());
		if (activeModule != module) {
			this.previousModule = this.activeModule;	
			this.activeModule = module;
			
			module.onActivate();
			moduleParentPanel.removeAll();
			if (module.getMainPanel() == null)
				throw new SystemException("Module does not have a main panel: " + module.getName());
			moduleParentPanel.add(module.getMainPanel());
			moduleParentPanel.validate();
			moduleParentPanel.repaint();
			
			moduleNavigatorPanel.removeAll();
			if (module.getNavigationPanel() != null) {
				moduleNavigatorPanel.add(module.getNavigationPanel());
			}
			moduleNavigatorPanel.validate();
			moduleNavigatorPanel.repaint();
		}
	}
	
	public void showPreviousModule() throws VectricsException {
		if (previousModule != null)
			showModule(previousModule);
		else {
			log.error("showPreviousModule() called, but previous module does not exist.");
			throw new VectricsException("showPreviousModule() called, but previous module does not exist.");
		} 
	}
	
}
